//
//  HeaderSearchView.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/8.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "HeaderSearchView.h"
@interface HeaderSearchView ()<UISearchBarDelegate>
{
    NSString * sectron_Text;
}
@end

@implementation HeaderSearchView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 2.0;
        self.layer.masksToBounds = YES;
        _search_View = [[SearchBarView alloc] initWithFrame:CGRectMake(0, 1, self.width, self.height)];
        _search_View.backgroundColor = [UIColor whiteColor];
        _search_View.delegate = self;
        //光标颜色
        _search_View.cursorColor = [UIColor blackColor];
        //清除按钮图标
        _search_View.clearButtonImage = [UIImage imageNamed:@"qingchu"];
        _search_View.searchBarTextField.backgroundColor = [UIColor whiteColor];
        _search_View.searchBarTextField.font = [UIFont systemFontOfSize:14];
        //输入框提示
        NSString *holderText = @"输入品名/材质/规格/店铺名";
        NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:holderText];
        [placeholder addAttribute:NSForegroundColorAttributeName
                            value:[UIColor colorWithHexString:@"#999999"]
                            range:NSMakeRange(0, holderText.length)];
        [placeholder addAttribute:NSFontAttributeName
                            value:[UIFont systemFontOfSize:14]
                            range:NSMakeRange(0, holderText.length)];
        _search_View.searchBarTextField.attributedPlaceholder = placeholder;
        //去掉取消按钮灰色背景
        _search_View.hideSearchBarBackgroundImage = YES;
        [self addSubview:_search_View];
    }
    return self;
}

-(void)setPlaceholderStr:(NSString *)placeholderStr{
    _placeholderStr = placeholderStr;
    _search_View.searchBarTextField.placeholder = _placeholderStr;
}

#pragma mark - UISearchBar Delegate
- (CGSize)intrinsicContentSize {
    return UILayoutFittingExpandedSize;
}
//已经开始编辑时的回调
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    if (self.delegate && [self.delegate respondsToSelector:@selector(beginAction)]) {
        [self.delegate beginAction];
    }
}

//编辑文字改变的回调
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    DLog(@"searchText:%@",searchText);
    sectron_Text = searchText;
    if (self.delegate && [self.delegate respondsToSelector:@selector(secrechActionText:theType:)]) {
        [self.delegate secrechActionText:sectron_Text theType:2];
    }
}

//搜索按钮
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    DLog(@"搜索");//
    if (self.delegate && [self.delegate respondsToSelector:@selector(secrechActionText:theType:)]) {
        [self.delegate secrechActionText:sectron_Text theType:1];
    }
}

@end
